//
//  GGTableViewController.swift
//  SwiftTest
//
//  Created by lisheng on 16/3/15.
//  Copyright © 2016年 鬼谷传人. All rights reserved.
//

import UIKit

protocol GGTableViewControllerDelegate:NSObjectProtocol {
    func closeButtonDidClicked()
    func tableViewDidSelectedAtIndexPath(indexPath:NSIndexPath)
}


class GGTableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    weak var delegate:GGTableViewControllerDelegate?
    
    let cellID = "cell_id"
    
    let width = UIScreen.mainScreen().bounds.size.width
    let height = UIScreen.mainScreen().bounds.size.height
    
    lazy var dataList = {
       return ["data0","data1","data2","data3","data4","data5","data6","data7","data8","data9","data10","data11"]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor(red: 0.854, green: 0.744, blue: 1.000, alpha: 0.373)
    
        initCenterView()

    }
    
    //MARK:init view
    func initCenterView() {
        let centerView  = UIView()
        centerView.backgroundColor = UIColor.whiteColor()
        centerView.center = view.center
        centerView.bounds = CGRect(x: 0, y: 0, width: width * 2 / 3, height: height * 2 / 3)
        view.addSubview(centerView)
        //label
        let label = UILabel()
        let labelHeight:CGFloat = 30.0
        label.backgroundColor = UIColor.grayColor()
        label.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(centerView.bounds), height: labelHeight)
        label.textAlignment = .Center
        label.textColor = UIColor.whiteColor()
        label.text = "Data List"
        centerView.addSubview(label)
        //button
        let button = UIButton(type: .Custom)
        let buttonHeight:CGFloat = 30.0
        button.frame = CGRect(x: 0, y: CGRectGetHeight(centerView.bounds) - buttonHeight, width: CGRectGetWidth(centerView.bounds), height: buttonHeight)
        button.addTarget(self, action: "closeView", forControlEvents: .TouchUpInside)
        button.setTitle("Close", forState: .Normal)
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.backgroundColor = UIColor.lightGrayColor()
        centerView.addSubview(button)
        //tableView
        let tableView = UITableView(frame: CGRect(x: 0, y: labelHeight, width: CGRectGetWidth(centerView.bounds), height: CGRectGetHeight(centerView.bounds) - labelHeight - buttonHeight), style: .Plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellID)
        centerView.addSubview(tableView)
    }
    
    func closeView() {
        print("close view")
        if ((delegate?.respondsToSelector("closeButtonDidClicked")) != nil) {
            delegate?.closeButtonDidClicked()
        }
        
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
        
        cell.textLabel?.text = dataList[indexPath.row]
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if ((delegate?.respondsToSelector("tableViewDidSelectedAtIndexPath:")) != nil) {
            delegate?.tableViewDidSelectedAtIndexPath(indexPath)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
  
}
